package au.com.betaversion.patientrecords.controller;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import au.com.betaversion.patientrecords.domain.BloodType;
import au.com.betaversion.patientrecords.domain.Child;
import au.com.betaversion.patientrecords.domain.Gender;
import au.com.betaversion.patientrecords.domain.Nationality;
import au.com.betaversion.patientrecords.domain.Patient;
import au.com.betaversion.patientrecords.domain.State;
import au.com.betaversion.patientrecords.domain.Suburb;
import au.com.betaversion.patientrecords.repository.BloodTypeRepository;
import au.com.betaversion.patientrecords.repository.GenderRepository;
import au.com.betaversion.patientrecords.repository.NationalityRepository;
import au.com.betaversion.patientrecords.repository.PatientRepository;
import au.com.betaversion.patientrecords.repository.StateRepository;
import au.com.betaversion.patientrecords.repository.SuburbRepository;
import au.com.betaversion.patientrecords.service.DateService;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("patient")
public class PatientController {
	
	private static final Logger logger = LoggerFactory.getLogger(PatientController.class);
	
	@Autowired private GenderRepository genderRepository;	
	@Autowired private BloodTypeRepository bloodTypeRepository;
	@Autowired private NationalityRepository nationalityRepository;
	@Autowired private PatientRepository patientRepository;
	@Autowired private StateRepository stateRepository;
	@Autowired private SuburbRepository suburbRepository;
	@Autowired private DateService dateService;

	
	//Common model attributes for the form
	
	@ModelAttribute("gender")
	public Iterable<Gender> getGenderList() {
		return genderRepository.findAll();
	}
	
	@ModelAttribute("bloodTypes")
	public Iterable<BloodType> getBloodTypes() {
		return bloodTypeRepository.findAll();
	}

	@ModelAttribute("nationalities")
	public Iterable<Nationality> getNationalities() {
		return nationalityRepository.findAll();
	}
	
	@ModelAttribute("states")
	public Iterable<State> getStates() {
		return stateRepository.findAll();
	}
	
	@ModelAttribute("suburbs")
	public Iterable<Suburb> getSuburbs() {
		return suburbRepository.findAll();
	}
	
	//------------------------------
	
	@RequestMapping(method = RequestMethod.GET)
	public String patientList(Model model) {
		Iterable<Patient> patients = patientRepository.findAll();
		
		for(Patient patient: patients) {
			patient.setAge(dateService.calculateAge(patient.getDateOfBirth()));
			for(Child child: patient.getChildren()) {
				child.setAge(dateService.calculateAge(child.getDateOfBirth()));
			}
		}
		
		model.addAttribute("patients", patients);
		return "patient";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public String patient(Model model) {
		model.addAttribute("patientForm", new Patient());
		return "patient-add";
	}
	
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public ModelAndView patientAdd(@ModelAttribute("patientForm") Patient patient, HttpServletRequest request ) {
		patientRepository.save(patient);
		logger.info("New patient " + patient.getFirstName() + " " + patient.getLastName() + " added.");
		return new ModelAndView(new RedirectView(request.getContextPath()));
	}
	
	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public ModelAndView patientDelete(@PathVariable("id") int id, HttpServletRequest request) {
		Patient patient = patientRepository.findOne(id);
		if(patient != null) {
			patientRepository.delete(patient);
			logger.info("Patient " + patient.getFirstName() + " " + patient.getLastName() + " deleted");
		}
		return new ModelAndView(new RedirectView(request.getContextPath()));
	}
	
	@InitBinder
	public void initBinder(WebDataBinder dataBinder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		CustomDateEditor dateEditor = new CustomDateEditor(dateFormat, false);
		dataBinder.registerCustomEditor(java.util.Date.class, dateEditor);
	}
	
}
